#include "../mqserver/queue.hpp"
#include <gtest/gtest.h>

mmq::MsgQueueManager::ptr mqmp;

class QueueTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        mqmp = std::make_shared<mmq::MsgQueueManager>("./data/meta.db");
    }
    virtual void TearDown() override
    {
        // mqmp->Clear();
    }
};

TEST(queue_test, insert_test)
{
    std::unordered_map<std::string, std::string> map = {{"k1", "v1"}, {"k2", "v2"}};
    // std::unordered_map<std::string, std::string> map;
    mqmp->DeclareQueue("queue1", true, true, false, map);
    mqmp->DeclareQueue("queue2", true, true, false, map);
    mqmp->DeclareQueue("queue3", true, true, false, map);
    mqmp->DeclareQueue("queue4", true, true, false, map);
    ASSERT_EQ(mqmp->Size(), 4); 
}

TEST(queue_test, select_test)
{
    // ASSERT_EQ(mqmp->Exists("queue1"), false);
    // ASSERT_EQ(mqmp->Exists("queue2"), true);
    // ASSERT_EQ(mqmp->Exists("queue3"), true);
    // ASSERT_EQ(mqmp->Exists("queue4"), true);
    mmq::MsgQueue::ptr mqp = mqmp->SelectQueue("queue2");
    ASSERT_NE(mqp.get(), nullptr);
    ASSERT_EQ(mqp->_name, "queue2");
    ASSERT_EQ(mqp->_durable, true);
    ASSERT_EQ(mqp->_exclusive, true);
    ASSERT_EQ(mqp->_auto_delete, false);
    ASSERT_EQ(mqp->GetArgs(), std::string("k2=v2&k1=v1&"));
}

TEST(queue_test, remove_test)
{
    mqmp->DeleteQueue("queue2");
    mmq::MsgQueue::ptr mqp = mqmp->SelectQueue("queue2");
    ASSERT_EQ(mqp.get(), nullptr);
    ASSERT_EQ(mqmp->Exists("queue2"), false);
}




int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new QueueTest);
    int ret = RUN_ALL_TESTS();
    return 0;
}